package com.example.common.controller;

import com.example.common.pojo.CompInfo;
import com.example.common.service.ICompInfoService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/compinfo")
public class CompInfoController {
    private final ICompInfoService iCompInfoService;
    @Autowired
    public CompInfoController(ICompInfoService iCompInfoService){
        this.iCompInfoService=iCompInfoService;
    }

    @RequestMapping("/find")
    @ResponseStatus(HttpStatus.OK)
    CompInfo getCompInfoByName(String name){
        return this.iCompInfoService.getCompInfoByName(name);
    }

    @RequestMapping("/delete/{id}")
    @ResponseStatus(HttpStatus.OK)
    int deleteCompInfo(@PathVariable("id") Integer id, String name){
        return this.iCompInfoService.deleteCompInfo(id,name);
    }

    @RequestMapping("/insert/{id}")
    @ResponseStatus(HttpStatus.OK)
    int insertCompInfo(@PathVariable("id") Integer id,@RequestBody CompInfo compInfo){
        if(this.getCompInfoByName(compInfo.getComName())==null){
            return this.iCompInfoService.insertCompInfo(id,compInfo);
        }
        else {
            return -1;
        }
    }

    @RequestMapping("/update/{id}")
    @ResponseStatus(HttpStatus.OK)
    int updateCompInfo(@PathVariable("id") Integer id,@RequestBody CompInfo compInfo){
        return this.iCompInfoService.updateCompInfo(id,compInfo);
    }
}
